/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.restaurant;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.restaurant.Food;
import com.boxin.ims.modules.wechat.dao.restaurant.FoodDao;

/**
 * 食品,菜品Service
 * @author Jakemanse
 * @version 2013-11-14
 */
@Component
@Transactional(readOnly = true)
public class FoodService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(FoodService.class);
	
	@Autowired
	private FoodDao foodDao;
	
	public Food get(Long id) {
		return foodDao.findOne(id);
	}
	
	public Page<Food> find(Page<Food> page, Food food) {
		DetachedCriteria dc = foodDao.createDetachedCriteria();
		if (food.getUser()!=null && food.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", food.getUser().getId()));
		}
		if (food.getCategory()!=null && food.getCategory().getId()>0){
			dc.createAlias("category", "category");
			dc.add(Restrictions.eq("category.id", food.getCategory().getId()));
		}
		if (StringUtils.isNotEmpty(food.getName())){
			dc.add(Restrictions.like("name", "%"+food.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(food.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+food.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", Food.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("sort"));
		return foodDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Food food) {
		food.setDetail(StringEscapeUtils.unescapeHtml4(food.getDetail()) );
		foodDao.save(food);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		foodDao.deleteById(id);
	}
	
}
